package com.tianyang.yukuaifuzs.business.trade.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.fastjson.JSONObject;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.tianyang.yukuaifuzs.R;
import com.tianyang.yukuaifuzs.base.BaseTitleActivity;
import com.tianyang.yukuaifuzs.business.trade.adapter.TradeRecordAdapter;
import com.tianyang.yukuaifuzs.business.trade.bean.TradeFilterInput;
import com.tianyang.yukuaifuzs.business.trade.bean.TradeRecordOutput;
import com.tianyang.yukuaifuzs.business.trade.contract.TradeRecordContract;
import com.tianyang.yukuaifuzs.business.trade.presenter.TradeRecordPresenter;
import com.tianyang.yukuaifuzs.business.trade.provider.TradeOpenProvider;
import com.tianyang.yukuaifuzs.common.interfaces.CommonConfig;
import com.tianyang.yukuaifuzs.module.dialog.TradeFilterDialog;
import com.tianyang.yukuaifuzs.module.dialog.interfaces.ICallbackListener;
import com.tianyang.yukuaifuzs.module.widget.VerticalSwipeRefreshLayout;
import com.tianyang.yukuaifuzs.utils.DateUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Description：交易记录
 * Created by xyj on 2024/3/11
 */
public class TradeRecordActivity extends BaseTitleActivity implements TradeRecordContract.View {

    @BindView(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;
    @BindView(R.id.recyclerView)
    RecyclerView vRecyclerview;

    private TradeRecordHeaderView vHeaderView;

    private RelativeLayout emptyView;

    private TextView filterTv;

    private TradeRecordAdapter vTradeRecordAdapter;

    private TradeFilterDialog vTradeFilterDialog;

    private TradeRecordContract.Presenter mPresenter;

    private TradeFilterInput mInput = new TradeFilterInput();

    private int nowPage = CommonConfig.PAGE_NUM;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_recycler_view;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void initView() {
        super.initView();
        commonTitle.setTitle("交易记录");

        vRecyclerview.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        vTradeRecordAdapter = new TradeRecordAdapter();
        vHeaderView = new TradeRecordHeaderView(mContext, null);
        emptyView = vHeaderView.findViewById(R.id.emptyView);
        filterTv = vHeaderView.findViewById(R.id.filterTv);
        vTradeRecordAdapter.addHeaderView(vHeaderView);
        vRecyclerview.setAdapter(vTradeRecordAdapter);
        vTradeRecordAdapter.disableLoadMoreIfNotFullPage(vRecyclerview);
        vTradeRecordAdapter.setPreLoadNumber(2);

        String startTime = DateUtils.getTimeAgo(30, "yyyy/MM/dd");
        String endTime = DateUtils.getCurrentTime("yyyy/MM/dd");
        mInput.setStartTime(startTime.replaceAll("/", "-"));
        mInput.setEndTime(endTime.replaceAll("/", "-"));
        filterTv.setText(startTime + "-" + endTime);

        mPresenter = new TradeRecordPresenter(this);
        mPresenter.getTradeRecordList(nowPage, mInput);
    }

    @Override
    protected void setListener() {
        super.setListener();
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                nowPage = CommonConfig.PAGE_NUM;
                mPresenter.getTradeRecordList(nowPage, mInput);
                vTradeRecordAdapter.setEnableLoadMore(true);
            }
        });
        vTradeRecordAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.getTradeRecordList(nowPage, mInput);
            }
        }, vRecyclerview);
        vTradeRecordAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                TradeRecordOutput output = ((TradeRecordAdapter) adapter).getItem(position);
                if (output != null) {
                    TradeOpenProvider.getInstance().openTradeDetailActivity(mContext, output, "交易详情");
                }
            }
        });
        filterTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (vTradeFilterDialog == null) {
                    vTradeFilterDialog = new TradeFilterDialog(mContext);
                    vTradeFilterDialog.setListener(new ICallbackListener<TradeFilterInput>() {
                        @Override
                        public void callback(TradeFilterInput input) {
                            mInput = input;
                            if (mInput != null) {
                                String startEndStr = mInput.getStartTime().replaceAll("-", "/") + "-" +
                                        mInput.getEndTime().replaceAll("-", "/");
                                filterTv.setText(startEndStr);
                            }
                            nowPage = CommonConfig.PAGE_NUM;
                            mPresenter.getTradeRecordList(nowPage, mInput);
                            vTradeRecordAdapter.setEnableLoadMore(true);
                        }
                    });
                }
                vTradeFilterDialog.show();
            }
        });
    }

    @Override
    public void showTradeTotalView(JSONObject jsonObject) {
        vHeaderView.showTradeTotalView(jsonObject);
    }

    @Override
    public void showTradeRecordListView(List<TradeRecordOutput> outputs, boolean isFirst) {
        if (outputs != null && outputs.size() > 0) {
            if (isFirst) {
                vTradeRecordAdapter.setNewData(outputs);
            } else {
                vTradeRecordAdapter.addData(outputs);
            }
            nowPage++;
            emptyView.setVisibility(View.GONE);
        } else if (isFirst) {
            vTradeRecordAdapter.setNewData(new ArrayList<>());
            emptyView.setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void showLoadMore(String loadMore) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        switch (loadMore) {
            case CommonConfig.LOADMORECOMPLETE:
                vTradeRecordAdapter.loadMoreComplete();
                break;
            case CommonConfig.LOADMOREEND:
                vTradeRecordAdapter.loadMoreEnd();
                break;
            case CommonConfig.LOADMOREFAIL:
                vTradeRecordAdapter.loadMoreFail();
                break;
        }
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    public void setPresenter(TradeRecordContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (vTradeFilterDialog != null && vTradeFilterDialog.isShowing()) {
            vTradeFilterDialog.dismiss();
            vTradeFilterDialog = null;
        }
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
